/*
 * MyEd.java
 *
 * Created on December 6, 2006, 6:28 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.netbeans.modules.manifesteditor.editor;

import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.text.Document;
import org.netbeans.core.spi.multiview.CloseOperationState;
import org.netbeans.core.spi.multiview.MultiViewElement;
import org.netbeans.core.spi.multiview.MultiViewElementCallback;
import org.netbeans.modules.manifesteditor.*;
import org.openide.text.CloneableEditor;
import org.openide.text.NbDocument;
import org.openide.util.Lookup;
import org.openide.util.Mutex;
import org.openide.windows.TopComponent;

/**
 *
 * @author Geertjan Wielenga
 */
public class ManifestSourceEditor extends CloneableEditor implements MultiViewElement, Runnable {
 
    private JComponent toolbar;
    private MultiViewElementCallback callback;
    
    public ManifestSourceEditor() {
    }
    
    ManifestSourceEditor(ManifestEditorSupport ed) {
        super(ed);
    }
    
    public JComponent getVisualRepresentation() {
        return this;
        //return null;
    }
    
    public JComponent getToolbarRepresentation() {
//        if (toolbar == null) {
//            JEditorPane pane = this.pane;
//            if (pane != null) {
//                Document doc = pane.getDocument();
//                if (doc instanceof NbDocument.CustomToolbar) {
//                    toolbar = ((NbDocument.CustomToolbar)doc).createToolbar(pane);
//                }
//            }
//            if (toolbar == null) {
//                //attempt to create own toolbar?
//                toolbar = new JPanel();
//            }
//        }
//        return toolbar;
        return null;
    }
    
    public void setMultiViewCallback(MultiViewElementCallback callback) {
        this.callback = callback;
        updateName();
    }
    
    public void componentOpened() {
        super.componentOpened();
    }
    
    public void componentClosed() {
        super.componentClosed();
    }
    
    public void componentShowing() {
        super.componentShowing();
    }
    
    public void componentHidden() {
        super.componentHidden();
    }
    
    public void componentActivated() {
        super.componentActivated();
    }
    
    public void componentDeactivated() {
        super.componentDeactivated();
    }
    
    public CloseOperationState canCloseElement() {
        return CloseOperationState.STATE_OK;
    }
    
    public void updateName() {
        Mutex.EVENT.readAccess(this);
    }
    
    public void run() {
        MultiViewElementCallback c = callback;
        if (c == null) {
            return;
        }
        TopComponent tc = c.getTopComponent();
        if (tc == null) {
            return;
        }
        
        super.updateName();
        tc.setName(this.getName());
        tc.setDisplayName(this.getDisplayName());
        tc.setHtmlDisplayName(this.getHtmlDisplayName());
    }
    
    public Lookup getLookup() {
        return ((ManifestDataObject)((ManifestEditorSupport)cloneableEditorSupport()).getDataObject()).getNodeDelegate().getLookup();
    }
}
